# -*- coding: utf-8 -*-

'''
Moduł zawierający definicję Dialogu umożliwiającego ustawienie parametrów algorytmu.

'''
from PyQt4 import QtGui 
from PyQt4 import QtCore
from numpy.core.numeric import Infinity

from gui import UserMatrixWidget

##Dialog pozwalający na ustawienie parametrów algorytmu.
class ParamDialog(QtGui.QDialog):
    ##Konstruktor klasy
    #@param penaltyMax maksymalna wartość kary za przerwę
    #@param matrixMinVal minimalna wartość możliwa do ustawienia w komórce macierzy
    #@param matrixMaxVal maksymalna wartość możliwa do ustawienia w komórce macierzy
    #@param nucleotideList lista nukleotydów
    #@param parent rodzic obiektu
    def __init__(self, penaltyMax, matrixMinVal, matrixMaxVal, nucleotideList, parent = None):
        QtGui.QDialog.__init__(self, parent)
        
        self.userMatrixWidget = UserMatrixWidget.UserMatrixWidget(nucleotideList, matrixMinVal, matrixMaxVal, parent)
                
        self.mainLayout = QtGui.QVBoxLayout(self)
        self.paramsLayout = QtGui.QGridLayout()
        self.buttonsLayout = QtGui.QHBoxLayout()
        self.mainLayout.addLayout(self.paramsLayout)
        self.mainLayout.addWidget(self.userMatrixWidget)
        self.mainLayout.addLayout(self.buttonsLayout)

        self.paramsLayout.addWidget(QtGui.QLabel('Penalty', self), 0, 0)
        self.penaltyValueSpinBox = QtGui.QDoubleSpinBox(self)
        self.penaltyValueSpinBox.setMaximum(penaltyMax)
        self.penaltyValueSpinBox.setMinimum(-Infinity)
        self.paramsLayout.addWidget(self.penaltyValueSpinBox, 0, 1)
        
        self.paramsLayout.addWidget(QtGui.QLabel('DNA Weight Matrix', self), 1, 0)
        self.matrixGroupBox = QtGui.QGroupBox(self)
        self.iubMatrixButton = QtGui.QRadioButton('IUB', self.matrixGroupBox)
        self.clustalwMatrixButton = QtGui.QRadioButton('ClustalW(1.6)', self.matrixGroupBox)
        self.userDefinedMatrixButton = QtGui.QRadioButton('User defined', self.matrixGroupBox)
        self.connect(self.userDefinedMatrixButton, QtCore.SIGNAL("toggled(bool)"), self._userButtonToggled)

        self.matrixGroupBoxLayout = QtGui.QVBoxLayout(self.matrixGroupBox)
        self.matrixGroupBoxLayout.addWidget(self.iubMatrixButton)
        self.matrixGroupBoxLayout.addWidget(self.clustalwMatrixButton)
        self.matrixGroupBoxLayout.addWidget(self.userDefinedMatrixButton)
        self.paramsLayout.addWidget(self.matrixGroupBox, 1, 1)
        self.matrixButtonGroup = QtGui.QButtonGroup(self)
        self.matrixButtonGroup.addButton(self.iubMatrixButton, 0)
        self.matrixButtonGroup.addButton(self.clustalwMatrixButton, 1)
        self.matrixButtonGroup.addButton(self.userDefinedMatrixButton, 2)
        
        self.okButton = QtGui.QPushButton('Ok', self)
        self.connect(self.okButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("accept()"))
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        self.connect(self.cancelButton, QtCore.SIGNAL("clicked()"), self,  QtCore.SLOT("reject()"))
        self.buttonsLayout.addWidget(self.okButton)
        self.buttonsLayout.addWidget(self.cancelButton)
        
        self.mainLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
        
    ##Redefinicja metody powodującej pojawienie się dialogu
    def exec_(self):
        ret = QtGui.QDialog.exec_(self)
        if ret == QtGui.QDialog.Rejected:
            self.userMatrixWidget.recoverMatrix()
        return ret
    
    ##Funkcja zwracająca ustawione w dialogu parametry
    #@return (wartość kary, indeks wybranej macierzy, macierz użytkownika)
    def getParams(self):
        penaltyVal = self.penaltyValueSpinBox.value()
        matrixNum = self.matrixButtonGroup.checkedId()
        matrix = None
        if matrixNum == 2:
            matrix = self.userMatrixWidget.getMatrix()
        return (penaltyVal, matrixNum, matrix)
        
    ##Funkcja ustawiająca dialog w zależności od podanych parametrów
    #@param penaltyVal wartość kary
    #@param matrixNum indeks macierzy
    def setParams(self, penaltyVal, matrixNum):
        self.penaltyValueSpinBox.setValue(penaltyVal)
        if matrixNum == 0:
            self.iubMatrixButton.setChecked(True)
            self.userMatrixWidget.setVisible(False)
        elif matrixNum == 1:
            self.clustalwMatrixButton.setChecked(True)
            self.userMatrixWidget.setVisible(False)
        else:
            self.userDefinedMatrixButton.setChecked(True)
            self.userMatrixWidget.setVisible(True)            
        
        
    def _userButtonToggled(self, checked):
        if checked == True:
            self.userMatrixWidget.setVisible(True)
        if checked == False:
            self.userMatrixWidget.setVisible(False)
